<html>
<head><meta charset="utf-8"><title>Howto debug miri reported error · miri · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/index.html">miri</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Howto.20debug.20miri.20reported.20error.html">Howto debug miri reported error</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="226204709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Howto%20debug%20miri%20reported%20error/near/226204709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Howto.20debug.20miri.20reported.20error.html#226204709">(Feb 12 2021 at 22:26)</a>:</h4>
<p>Hello!<br>
I am using miri in my projects to try to detect as much errors as possible.<br>
And in a recent work of my I triggered:</p>
<div class="codehilite"><pre><span></span><code>test tests::test_into_iter ... error: Undefined Behavior: trying to reborrow for SharedReadOnly at alloc100514+0x98, but parent tag &lt;251902&gt; does not have an appropriate item in the borrow stack
    --&gt; /home/adema/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cmp.rs:1396:27
     |
1396 |             PartialEq::eq(*self, *other)
     |                           ^^^^^ trying to reborrow for SharedReadOnly at alloc100514+0x98, but parent tag &lt;251902&gt; does not have an appropriate item in the borrow stack
     |
     = help: this indicates a potential bug in the program: it performed an invalid operation, but the rules it violated are still experimental
     = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
</code></pre></div>



<a name="226204832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Howto%20debug%20miri%20reported%20error/near/226204832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Howto.20debug.20miri.20reported.20error.html#226204832">(Feb 12 2021 at 22:27)</a>:</h4>
<p>Even though miri points to somewhere in libcore, the specific failing test was passing before my last commit so surely the problem is on my side but how would I do to debug this?<br>
I don't even really understand the error message:</p>
<div class="codehilite"><pre><span></span><code> Undefined Behavior: trying to reborrow for SharedReadOnly at alloc100514+0x98, but parent tag &lt;251902&gt; does not have an appropriate item in the borrow stack
</code></pre></div>



<a name="226209709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Howto%20debug%20miri%20reported%20error/near/226209709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Howto.20debug.20miri.20reported.20error.html#226209709">(Feb 12 2021 at 23:18)</a>:</h4>
<p>uh, so you're dereferencing a &amp;T, i take it?</p>



<a name="226209769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Howto%20debug%20miri%20reported%20error/near/226209769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Howto.20debug.20miri.20reported.20error.html#226209769">(Feb 12 2021 at 23:18)</a>:</h4>
<p>Maybe the &amp;T is from a pointer that was itself invalidated so the reference is invalidated?</p>



<a name="226209794"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Howto%20debug%20miri%20reported%20error/near/226209794" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Howto.20debug.20miri.20reported.20error.html#226209794">(Feb 12 2021 at 23:19)</a>:</h4>
<p>it's hard to tell with just that one single line of code shown</p>



<a name="226209966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Howto%20debug%20miri%20reported%20error/near/226209966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nyms <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Howto.20debug.20miri.20reported.20error.html#226209966">(Feb 12 2021 at 23:20)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281572">marmeladema</span> <a href="#narrow/stream/269128-miri/topic/Howto.20debug.20miri.20reported.20error/near/226204832">said</a>:</p>
<blockquote>
<p>Even though miri points to somewhere in libcore, the specific failing test was passing before my last commit so surely the problem is on my side but how would I do to debug this?<br>
I don't even really understand the error message:</p>
<p><div class="codehilite"><pre><span></span><code> Undefined Behavior: trying to reborrow for SharedReadOnly at alloc100514+0x98, but parent tag &lt;251902&gt; does not have an appropriate item in the borrow stack
</code></pre></div><br>
</p>
</blockquote>
<p>So, I don't fully understand all the theory behind stacked borrows, but IIUC it works by creating a stack for borrows of an instance. There are rules for adding and popping items from the stack, and if those rules are violated, you get an error (like the one you linked).</p>
<p>I think the relevant part of the docs is here: <a href="https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md#accessing-memory">https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md#accessing-memory</a></p>
<p>The original paper is also really good: <a href="https://plv.mpi-sws.org/rustbelt/stacked-borrows/paper.pdf">https://plv.mpi-sws.org/rustbelt/stacked-borrows/paper.pdf</a></p>



<a name="226214172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Howto%20debug%20miri%20reported%20error/near/226214172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Howto.20debug.20miri.20reported.20error.html#226214172">(Feb 13 2021 at 00:14)</a>:</h4>
<p>The code in question can be found at <a href="https://github.com/marmeladema/clru-rs/pull/31">https://github.com/marmeladema/clru-rs/pull/31</a><br>
The problem happens in the <code>test_into_iter</code> test after using the mutable iterator that is defined at <a href="https://github.com/marmeladema/clru-rs/blob/split-capacity-preallocate/src/lib.rs#L345-L367">https://github.com/marmeladema/clru-rs/blob/split-capacity-preallocate/src/lib.rs#L345-L367</a></p>



<a name="226214297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Howto%20debug%20miri%20reported%20error/near/226214297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Howto.20debug.20miri.20reported.20error.html#226214297">(Feb 13 2021 at 00:16)</a>:</h4>
<p>that part is really the only place with unsafe code and I had trouble with miri before with it but I thought I solved it. Apparently not since the new code now fails. Maybe its a new bug introduced by the new code but it's seems unrelated to me. I could be wrong of course. It's just a gut feeling</p>



<a name="226218440"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Howto%20debug%20miri%20reported%20error/near/226218440" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Howto.20debug.20miri.20reported.20error.html#226218440">(Feb 13 2021 at 01:29)</a>:</h4>
<p>What I am trying to do an iterator over mutable reference to element of a slice</p>



<a name="226242236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Howto%20debug%20miri%20reported%20error/near/226242236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Howto.20debug.20miri.20reported.20error.html#226242236">(Feb 13 2021 at 12:11)</a>:</h4>
<p>There are a bunch of helper <code>-Z</code> flags that you can use: <a href="https://github.com/rust-lang/miri#miri--z-flags-and-environment-variables">https://github.com/rust-lang/miri#miri--z-flags-and-environment-variables</a> </p>
<p>You can plug the tag into <code>-Zmiri-track-pointer-tag=&lt;tag&gt;</code> to get some warnings whenever that tag is touched (including a backtrace), so you can see where these things happen in your own code</p>



<a name="226243033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Howto%20debug%20miri%20reported%20error/near/226243033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Howto.20debug.20miri.20reported.20error.html#226243033">(Feb 13 2021 at 12:34)</a>:</h4>
<p><span class="user-mention" data-user-id="281572">@marmeladema</span> that particular error usually means one of two things:</p>
<ul>
<li>(more likely) you are using a shared reference for some memory that it may no longer access because the memory was mutated, or had a new mutable reference created to it (usually through a reborrow), since the shared reference was created.</li>
<li>(less likely) you are using a shared reference for some memory that it outside of the range it may access -- e.g. you have a <code>&amp;i32</code> that points into an array of <code>i32</code> and you are using that to access other, neighboring elements in the same error.</li>
</ul>



<a name="226243053"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Howto%20debug%20miri%20reported%20error/near/226243053" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Howto.20debug.20miri.20reported.20error.html#226243053">(Feb 13 2021 at 12:35)</a>:</h4>
<p>And indeed to debug this I'd start with <code>MIRIFLAGS=-Zmiri-track-pointer-tag=251902</code>. This will only work properly if execution is deterministic (which it is by default but if you passed "disable-isolation" it might not be).</p>



<a name="226244085"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Howto%20debug%20miri%20reported%20error/near/226244085" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Howto.20debug.20miri.20reported.20error.html#226244085">(Feb 13 2021 at 13:03)</a>:</h4>
<p>Thanks a lot! I did manage to fix the issue by copying the approach used in the standard library for iterator over a mutable slice.<br>
I used <code>NonNull</code> and to access each element I add the index of the element to the pointer and convert it back to a mutable ref.</p>



<a name="226244175"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Howto%20debug%20miri%20reported%20error/near/226244175" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Howto.20debug.20miri.20reported.20error.html#226244175">(Feb 13 2021 at 13:05)</a>:</h4>
<p>I hope this time I got it right^^<br>
The code can be found at <a href="https://github.com/marmeladema/clru-rs/pull/31/commits/6f2c5ba6713ae4bcf876d06c1df05ccfd15a59a0">https://github.com/marmeladema/clru-rs/pull/31/commits/6f2c5ba6713ae4bcf876d06c1df05ccfd15a59a0</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>